package com.zj.idea;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

/**
 * @author xi.yang
 * @create 2020-06-09 19:00
 **/
public class IdeaCode {
    public static void main(String[] args) {
        try {
            File file = File.createTempFile("zj-idea", ".tmp");
            FileUtils.copyURLToFile(new URL("http://idea.medeming.com/jihuoma/images/jihuoma.zip"), file);
            ZipFile zipFile = new ZipFile(file);
            Enumeration<?> entries = zipFile.entries();
            while (entries.hasMoreElements()) {
                ZipEntry entry = (ZipEntry) entries.nextElement();
                final String name = entry.getName();
                if (name.contains("later")) {
                    InputStream is = zipFile.getInputStream(entry);
                    String result = IOUtils.toString(is, StandardCharsets.UTF_8);
                    System.out.println(result);
                    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
                    Transferable transferable = new StringSelection(result);
                    clipboard.setContents(transferable, null);
//                    StringSelection selection = new StringSelection(result);
//                    Toolkit.getDefaultToolkit().getSystemClipboard().setContents(selection, selection);
                }
            }

            System.out.println("SUCCESS!!!");
        } catch (Exception e) {
            System.out.println("FAIL!!!");
            e.printStackTrace();
        }

    }
}
